import Vue from 'vue'
import VueRouter from 'vue-router'

import Layout from '@/layout'

// import Login from '@/components/login/login.vue'

Vue.use(VueRouter)

const constantRoutes = [
  {
    path:'/',
    component:Layout,
    redirect:'/home',
    meta:{title:'首页',rights:['admin','user']},
    children:[
      {path:'/home',meta:{title:'首页',rights:['admin']},component:()=>import('@/views/home')},
      {path:'/user',meta:{title:'用户管理',rights:['admin','user']},component:()=>import('@/views/home')},
    ]
  },
  {
    path:'/other',
    component:Layout,
    meta:{title:'其他',rights:['admin','user']},
    redirect:'/other/other1',
    children:[
      {path:'other1',meta:{title:'其他1',rights:['admin','user']},component:()=>import('@/views/home')},
      {path:'other2',meta:{title:'其他2',rights:['admin']},component:()=>import('@/views/home')},
    ]
  },
  {
    path:'/device',
    component:Layout,
    meta:{title:'设备',rights:['admin','user']},
    redirect:'/device/device1',
    children:[
      {path:'device1',meta:{title:'设备1',rights:['admin','user']},component:()=>import('@/views/home')},
      {path:'device2/device21',meta:{title:'设备21',rights:['admin']}, component:()=>import('@/views/home')},
    ]
  },
]
let role='user'
let newroutes=[]
const handleRoutes=(routes)=>{
  routes.forEach(v=>{
    if(v.meta.rights.includes(role)){
      if(v.children){
        handleRoutes(v.children)
      }
      newroutes.push(v)
    }
  })
}
handleRoutes(constantRoutes)

console.log(newroutes,999);

const router = new VueRouter({
  // mode: 'hash',
  mode: 'history',
  base: process.env.BASE_URL,
  // routes
  routes: constantRoutes
})

export default router
